package com.java110.mall.api.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.cache.Java110RedisConfig;
import com.java110.dto.privilege.BasePrivilegeDto;
import com.java110.dto.privilege.PrivilegeUserDto;
import com.java110.dto.shop.ShopDto;
import com.java110.dto.user.StoreStaffDto;
import com.java110.intf.shop.IShopInnerServiceSMO;
import com.java110.intf.user.IMenuV1InnerServiceSMO;
import com.java110.intf.user.IPrivilegeUserV1InnerServiceSMO;
import com.java110.intf.user.IStoreStaffV1InnerServiceSMO;
import com.java110.mall.api.service.IGetCommunityStoreInfoSMO;
import com.java110.utils.util.Assert;
import com.java110.utils.util.ListUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class GetCommunityStoreInfoSMOImpl implements IGetCommunityStoreInfoSMO {

    private static final String BASE_PRIVILEGE = "JAVA110_BASE_PRIVILEGE";

    @Autowired
    private IStoreStaffV1InnerServiceSMO storeStaffV1InnerServiceSMOImpl;

    @Autowired
    private IShopInnerServiceSMO shopInnerServiceSMOImpl;

    @Autowired
    private IPrivilegeUserV1InnerServiceSMO privilegeUserV1InnerServiceSMOImpl;

    @Autowired
    private IMenuV1InnerServiceSMO menuV1InnerServiceSMOImpl;

    @Override
    @Cacheable(value = "getStoreInfo" + Java110RedisConfig.GET_STORE_INFO_EXPIRE_TIME_KEY, key = "#userId")
    public StoreStaffDto getStoreInfo(String userId) {

        Assert.hasLength(userId, "用户未登录请先登录");
        StoreStaffDto storeStaffDto = new StoreStaffDto();
        storeStaffDto.setStaffId(userId);
        List<StoreStaffDto> storeStaffDtos = storeStaffV1InnerServiceSMOImpl.queryStoreStaffs(storeStaffDto);

        if (ListUtil.isNull(storeStaffDtos)) {
            return null;
        }

        return storeStaffDtos.get(0);
    }

    @Override
    @Cacheable(value = "getStoreEnterShops" + Java110RedisConfig.GET_STORE_ENTER_COMMUNITYS_EXPIRE_TIME_KEY, key = "#storeId")
    public List<ShopDto> getStoreEnterShops(String storeId, String storeTypeCd) {

        ShopDto shopDto = new ShopDto();
        shopDto.setStoreId(storeId);
        List<ShopDto> shopDtos = shopInnerServiceSMOImpl.queryShops(shopDto);
        if(ListUtil.isNull(shopDtos)){
            throw new IllegalArgumentException("商户未包含商铺");
        }
        return shopDtos;
    }

    @Override
    @Cacheable(value = "getUserPrivileges" + Java110RedisConfig.DEFAULT_EXPIRE_TIME_KEY, key = "#staffId")
    public List<PrivilegeUserDto> getUserPrivileges( String staffId, String storeTypeCd) {

        PrivilegeUserDto privilegeUserDto = new PrivilegeUserDto();
        privilegeUserDto.setUserId(staffId);
        privilegeUserDto.setDomain(storeTypeCd);
        List<PrivilegeUserDto> privilegeUserDtos = privilegeUserV1InnerServiceSMOImpl.getUserPrivileges(privilegeUserDto);
        return privilegeUserDtos;


    }

    @Cacheable(value = "checkUserHasResourceListener" + Java110RedisConfig.DEFAULT_EXPIRE_TIME_KEY, key = "#cacheKey")
    public List<Map> checkUserHasResourceListener( JSONObject paramIn, String cacheKey) {
        ResponseEntity<String> responseEntity = null;
        BasePrivilegeDto basePrivilegeDto = new BasePrivilegeDto();
        basePrivilegeDto.setResource(paramIn.getString("resource"));
        basePrivilegeDto.setUserId(paramIn.getString("userId"));
        List<Map> privileges = menuV1InnerServiceSMOImpl.checkUserHasResource(basePrivilegeDto);

        return privileges;

    }
}
